#ifndef BASE_UTILS_INCLUDE_ALIGNED_MEMORY_H
#define BASE_UTILS_INCLUDE_ALIGNED_MEMORY_H

#include "platform_abstraction.h"
#include <cstdlib>
#include <memory>

namespace Base {
namespace Utils {

class AlignedMemory {
public:
    AlignedMemory(size_t size, size_t alignment);
    ~AlignedMemory();
    
    void* get() const { return ptr_; }
    size_t size() const { return size_; }
    size_t alignment() const { return alignment_; }
    
    // 禁用拷贝
    AlignedMemory(const AlignedMemory&) = delete;
    AlignedMemory& operator=(const AlignedMemory&) = delete;
    
    // 允许移动
    AlignedMemory(AlignedMemory&& other) noexcept;
    AlignedMemory& operator=(AlignedMemory&& other) noexcept;

private:
    void* ptr_ = nullptr;
    size_t size_ = 0;
    size_t alignment_ = 0;
};

// 辅助函数创建对齐内存的unique_ptr
template<typename T>
std::unique_ptr<T, void(*)(void*)> MakeAlignedUnique(size_t alignment, size_t count = 1) {
    size_t size = sizeof(T) * count;
    void* memory = Platform::MemoryAllocateAligned(size, alignment);
    auto deleter = [](void* ptr) { Platform::MemoryFreeAligned(ptr); };
    
    return std::unique_ptr<T, void(*)(void*)>(static_cast<T*>(memory), deleter);
}

} // namespace Utils
} // namespace Base

#endif // BASE_UTILS_INCLUDE_ALIGNED_MEMORY_H
